import { defineStore } from 'pinia'
import { ref, computed, watch } from 'vue'
import type { DeviceConfig, DeviceNode } from '@/types/device'
import { useDeviceReaderStore } from '@/stores/device/reader'
import { useDeviceWriterStore } from '@/stores/device/writer'

export const useDeviceManagerStore = defineStore('device-manager', () => {
  const readerStore = useDeviceReaderStore()
  const writerStore = useDeviceWriterStore()

  // plc 类型列表
  const deviceTypes = [
    'SiemensS1500',
    'SiemensS1200',
    'SiemensS300',
    'SiemensS400',
    'SiemensS200',
    'OmronFins',
    'OmronCIP'
  ]

  // 数据类型
  const dataTypes = ['bool', 'byte', 'short', 'ushort', 'int', 'uint', 'float', 'double']

  // 节点名称列表
  const nodeIds = ref<string[]>()

  // 已经设置节点名称列表
  const alreadySetNodeIds = ref(false)

  watch(nodeIds, (newValue, oldValue) => {
    alreadySetNodeIds.value = true
  })

  // 当前选择的节点 id
  const currentNodeId = ref<string>('')

  // 重置 reader 的值
  watch(currentNodeId, (newValue, oldValue) => {
    readerStore.reset()
    writerStore.reset()
  })

  // 设备节点
  const currentNode = ref<DeviceNode | null>(null)

  // 重置 reader 的值
  watch(currentNode, (newValue, oldValue) => {
    readerStore.reset()
    writerStore.reset()
  })

  // 获取当前设备配置
  const currentConfigs = computed<DeviceConfig[]>(() => {
    if (
      currentNode.value == null ||
      currentNode.value.deviceConfigs == null ||
      currentNode.value.deviceConfigs.length == 0
    ) {
      return []
    } else {
      return currentNode.value.deviceConfigs
    }
  })

  // 获取当前设备配置
  const deviceNames = computed<string[]>(() => {
    if (currentConfigs.value.length == 0) {
      return []
    } else {
      if (currentNode.value == null) {
        return []
      } else {
        return currentNode.value.deviceConfigs.map((item) => item.deviceName)
      }
    }
  })

  // 是否存在配置
  const hasConfig = computed(() => {
    if (
      currentNode.value == null ||
      currentNode.value.deviceConfigs == null ||
      currentNode.value.deviceConfigs.length == 0
    ) {
      return false
    } else {
      return true
    }
  })

  // 从当前设备移除掉配置
  function removeConfigFromCurrentNode(index: number) {
    if (
      currentNode.value != null &&
      currentNode.value.deviceConfigs != null &&
      currentNode.value.deviceConfigs.length > 0 &&
      index >= 0 &&
      index < currentNode.value.deviceConfigs.length
    ) {
      // 移除后需要与数据库同步
      return currentNode.value.deviceConfigs.splice(index, 1)
    }
  }

  function addConfigToCurrentNode(newConfig: DeviceConfig) {
    if (currentNode.value != null) {
      if (currentNode.value.deviceConfigs != null) {
        // 添加后与数据库同步
        currentNode.value.deviceConfigs.push(newConfig)
      }
    }
  }

  return {
    deviceTypes,
    dataTypes,
    nodeIds,
    alreadySetNodeIds,
    deviceNames,
    currentNodeId,
    currentNode,
    hasConfig,
    currentConfigs,
    removeConfigFromCurrentNode,
    addConfigToCurrentNode
  }
})
